package com.briup.oagg.dataset.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.framework.utils.web.Response;
import com.briup.oagg.dataset.bean.basic.Basin;
import com.briup.oagg.dataset.bean.ext.TreeInfo;
import com.briup.oagg.dataset.service.IBasinService;
import com.briup.oagg.dataset.web.vm.BasinAddVM;
import com.briup.oagg.dataset.web.vm.BasinUpdateVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 盆地信息业务前端控制器
 *
 * @author briup
 */
@Validated
@Api(tags = "1.盆地管理")
@RestController
@RequestMapping("/basin")
public class BasinController {
    @Autowired
    private IBasinService service;

    @ApiOperation(value = "新增盆地", notes = "通过输入名称和特征描述信息保存信息。输入信息都必填。名称不能重复。")
    @PostMapping
    public Response addBasin(@RequestBody @Valid BasinAddVM vm) {
        service.addBasin(vm);
        return Response.success();
    }

    @ApiOperation(value = "修改盆地", notes = "通过输入盆地的ID,只能修改盆地的描述信息")
    @PutMapping
    public Response updateBasin(@RequestBody @Valid BasinUpdateVM vm) {
        service.updateBasin(vm);
        return Response.success();
    }

    @ApiOperation(value = "根据盆地编号删除盆地", notes = "根据盆地编号删除盆地信息及其对应的区带、圈闭和油气藏信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "basinId", value = "盆地ID")
    })
    @DeleteMapping("/{basinId}")
    public Response deleteById(@PathVariable @NotBlank(message = "盆地ID不能为空") String basinId) {
        service.deleteById(basinId);
        return Response.success();
    }

    @ApiOperation(value = "查询盆地总数")
    @GetMapping("/count")
    public Response<Integer> countNum() {
        int num = service.countNum();
        return Response.success(num);
    }

    @ApiOperation(value = "分页查询盆地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码",defaultValue = "1",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页大小",defaultValue = "5",required = true,dataTypeClass = Integer.class)
    })
    @GetMapping("/page")
    public Response<Page<Basin>> findByPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        Page<Basin> page = service.findByPage(pageNum, pageSize);
        return Response.success(page);
    }

    @ApiOperation(value = "查询地质信息树")
    @GetMapping("/tree")
    public Response<List<TreeInfo>> findByTree() {
        List<TreeInfo> list = service.findByTree();
        return Response.success(list);
    }
}
